/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package view.ihm;

import view.com.PgButton;
import mod.dao.membre;
import mod.dao.tables;
import java.awt.BorderLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.sql.ResultSet;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import ctrl.sys.config;
import ctrl.jeu.Chaise;
import javax.swing.JApplet;

/**
 *
 * @author willy
 */
public class guiTable extends JPanel implements ActionListener{

    Object[][] data;
    private PgButton link;
    JTable tableau;
    membre pers;
    JApplet high;

    public guiTable(membre m, JApplet par)
    {
        high=par;
        this.setLayout(new BorderLayout());
        pers=m;
        //Les données du tableau
        ResultSet rs=new tables().Liste();

        int nbre=0;
        try
        {
            rs.last();
            nbre=rs.getRow();
        }catch(Exception e) {e.printStackTrace();}

        link=new PgButton("Connecter");
        link.addActionListener(this);
        data=new Object[nbre][5];
        //System.out.println(" nbre= "+nbre);
        try {
            int i=0;
            rs.beforeFirst();
            while(rs.next())
            {
                data[i][0]=rs.getString("id");
                data[i][1]=rs.getString("nom");
                data[i][2]=rs.getString("blind");
                data[i][3]=rs.getString("max");
                data[i][4]=rs.getString("min");
                i++;
            }
        }catch(Exception e) {}
        
        //Les titres des colonnes
        String  title[] = {"ID", "Nom de la table", "Blind", "Maximum", "Minimum"};

        tableau = new JTable(data, title);
        //On ajoute notre tableau à notre contentPane dans un scroll
        //Sinon les titres des colonnes ne s'afficheront pas ! !	
        add(new JScrollPane(tableau),BorderLayout.CENTER);
        
        JPanel bas=new JPanel();
        
        bas.add(link);
        add(bas,BorderLayout.SOUTH);

        JPanel haut=new JPanel();
        ImageIcon imgi=null;
        try{
            try {
                imgi=new javax.swing.ImageIcon(new URL(pers.getPHOTO()));
            }catch (Exception e)
            {
                if(pers.getSEXE().equalsIgnoreCase("masculin")||pers.getSEXE().equalsIgnoreCase("homme"))
                    imgi=new javax.swing.ImageIcon(getClass().getResource("/img/defauthomme.png"));
                else
                    imgi=new javax.swing.ImageIcon(getClass().getResource("/img/defautfemme.png"));
            }
            
            Image im=Chaise.scale(imgi.getImage(), 150, 150);
            imgi=new ImageIcon(im);
        }catch(Exception e) {e.printStackTrace();}
        if(imgi!=null) haut.add(new JLabel(imgi));

        // affichage des infos du profil
        JPanel info=new JPanel();
        info.setLayout(new BoxLayout(info,BoxLayout.Y_AXIS));
        info.add(new JLabel("Nom : "+pers.getPRENOM()+" "+pers.getNOM()));
        info.add(new JLabel("Sexe : "+pers.getSEXE()));
        info.add(new JLabel("Date naissance : "+pers.getDATENAIS()));
        info.add(new JLabel("Ville : "+pers.getVILLE()));
        info.add(new JLabel("Montant Actuel : "+pers.getMONTANT()));
        haut.add(info);
        add(haut,BorderLayout.NORTH);
        
    }

    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==link)
        {
            if(tableau.getSelectedRow()!=-1)
            {
                Integer idSelect= new Integer(tableau.getValueAt(tableau.getSelectedRow(),0).toString());
                Load jeu=new Load(config.serveurSocket,idSelect,pers,high);
                high.setSize(800,600);
                high.add(jeu);
                high.remove(this);
                high.repaint();
                //System.out.println("ligne = "+idSelect);
                //jeu.setVisible(true);
                jeu.Entree();
                
            }
        }
    }


    public static void main(String[] args)
    {
        JFrame jf=new JFrame("P.G : Liste des tables");
        //jf.add(new guiTable());
        jf.setSize(600,400);
        jf.setVisible(true);
    }
}
